/**
 *
 */
package anysware.gwt.client.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.google.gwt.http.client.URL;

/**
 * TODO: doc me
 *
 * @author keke<keke@codehaus.org>
 * @revision $Revision: 110 $
 * @version
 */
public class QueryBuilder {
	private Map<String, String>	params;

	public QueryBuilder add(final String name, final String value) {
		if (params == null) {
			params = new HashMap<String, String>();
		}
		params.put(name, value);
		return this;
	}

	public String build() {
		final StringBuffer sb = new StringBuffer();
		boolean first = true;
		for (final Entry<String, String> entry : params.entrySet()) {
			if (!first) {
				sb.append("&");
			}
			sb.append(URL.encodeComponent(entry.getKey())).append("=").append(
			        URL.encodeComponent(entry.getValue()));
			first = false;
		}
		return sb.toString();
	}
}
